﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class admin_AD_Operation : System.Web.UI.Page
{
    string adSql;
    protected void Page_Load(object sender, EventArgs e)
    {
        AdminCs.CheckLoginState();
        if (!this.IsPostBack)
        {
            if (Request.QueryString["ad"] == "edit")
            {
                ad_txt();
            }
            else if (Request.QueryString["ad"] == "del")
            {
                ad_del();
            }
        }
    }
    public void ad_del()//删除信息
    {
        int id = int.Parse(Request.QueryString["ID"]);
        adSql = "delete from AD where ad_ID=" + id;
        Class1.ExecSql(adSql);
        Class1.AlertShow("删除成功！", "AD_Manage.aspx");
    }
    public void ad_txt()//文本信息
    {
        int id = int.Parse(Request.QueryString["ID"]);
        DataTable dt = Class1.ExecSel("select * from AD where ad_ID='" + id + "'");
        TextBox1.Text = Convert.ToString(dt.Rows[0]["ad_Url"]);
        TextBox2.Text = Convert.ToString(dt.Rows[0]["ad_Img"]);

        #region 
        if (dt.Rows[0]["ad_Location"].ToString() == "0")
        {
            this.RadioButtonList1.Items[0].Selected = true;
            this.RadioButtonList1.Items[1].Selected = false;
            this.RadioButtonList1.Items[2].Selected = false;
            this.RadioButtonList1.Items[3].Selected = false;
            this.RadioButtonList1.Items[4].Selected = false;
            this.RadioButtonList1.Items[5].Selected = false;
            this.RadioButtonList1.Items[6].Selected = false;
            this.RadioButtonList1.Items[7].Selected = false;
            this.RadioButtonList1.Items[8].Selected = false;
        }
        else if (dt.Rows[0]["ad_Location"].ToString() == "1")
        {
            this.RadioButtonList1.Items[0].Selected = false;
            this.RadioButtonList1.Items[1].Selected = true;
            this.RadioButtonList1.Items[2].Selected = false;
            this.RadioButtonList1.Items[3].Selected = false;
            this.RadioButtonList1.Items[4].Selected = false;
            this.RadioButtonList1.Items[5].Selected = false;
            this.RadioButtonList1.Items[6].Selected = false;
            this.RadioButtonList1.Items[7].Selected = false;
            this.RadioButtonList1.Items[8].Selected = false;
        }
        else if (dt.Rows[0]["ad_Location"].ToString() == "2")
        {
            this.RadioButtonList1.Items[0].Selected = false;
            this.RadioButtonList1.Items[1].Selected = false;
            this.RadioButtonList1.Items[2].Selected = true;
            this.RadioButtonList1.Items[3].Selected = false;
            this.RadioButtonList1.Items[4].Selected = false;
            this.RadioButtonList1.Items[5].Selected = false;
            this.RadioButtonList1.Items[6].Selected = false;
            this.RadioButtonList1.Items[7].Selected = false;
            this.RadioButtonList1.Items[8].Selected = false;
        }
        else if (dt.Rows[0]["ad_Location"].ToString() == "3")
        {
            this.RadioButtonList1.Items[0].Selected = false;
            this.RadioButtonList1.Items[1].Selected = false;
            this.RadioButtonList1.Items[2].Selected = false;
            this.RadioButtonList1.Items[3].Selected = true;
            this.RadioButtonList1.Items[4].Selected = false;
            this.RadioButtonList1.Items[5].Selected = false;
            this.RadioButtonList1.Items[6].Selected = false;
            this.RadioButtonList1.Items[7].Selected = false;
            this.RadioButtonList1.Items[8].Selected = false;
        }
        else if (dt.Rows[0]["ad_Location"].ToString() == "4")
        {
            this.RadioButtonList1.Items[0].Selected = false;
            this.RadioButtonList1.Items[1].Selected = false;
            this.RadioButtonList1.Items[2].Selected = false;
            this.RadioButtonList1.Items[3].Selected = false;
            this.RadioButtonList1.Items[4].Selected = true;
            this.RadioButtonList1.Items[5].Selected = false;
            this.RadioButtonList1.Items[6].Selected = false;
            this.RadioButtonList1.Items[7].Selected = false;
            this.RadioButtonList1.Items[8].Selected = false;
        }
        else if (dt.Rows[0]["ad_Location"].ToString() == "5")
        {
            this.RadioButtonList1.Items[0].Selected = false;
            this.RadioButtonList1.Items[1].Selected = false;
            this.RadioButtonList1.Items[2].Selected = false;
            this.RadioButtonList1.Items[3].Selected = false;
            this.RadioButtonList1.Items[4].Selected = false;
            this.RadioButtonList1.Items[5].Selected = true;
            this.RadioButtonList1.Items[6].Selected = false;
            this.RadioButtonList1.Items[7].Selected = false;
            this.RadioButtonList1.Items[8].Selected = false;
        }
        else if (dt.Rows[0]["ad_Location"].ToString() == "6")
        {
            this.RadioButtonList1.Items[0].Selected = false;
            this.RadioButtonList1.Items[1].Selected = false;
            this.RadioButtonList1.Items[2].Selected = false;
            this.RadioButtonList1.Items[3].Selected = false;
            this.RadioButtonList1.Items[4].Selected = false;
            this.RadioButtonList1.Items[5].Selected = false;
            this.RadioButtonList1.Items[6].Selected = true;
            this.RadioButtonList1.Items[7].Selected = false;
            this.RadioButtonList1.Items[8].Selected = false;
        }
        else if (dt.Rows[0]["ad_Location"].ToString() == "7")
        {
            this.RadioButtonList1.Items[0].Selected = false;
            this.RadioButtonList1.Items[1].Selected = false;
            this.RadioButtonList1.Items[2].Selected = false;
            this.RadioButtonList1.Items[3].Selected = false;
            this.RadioButtonList1.Items[4].Selected = false;
            this.RadioButtonList1.Items[5].Selected = false;
            this.RadioButtonList1.Items[6].Selected = false;
            this.RadioButtonList1.Items[7].Selected = true;
            this.RadioButtonList1.Items[8].Selected = false;
        }
        else if (dt.Rows[0]["ad_Location"].ToString() == "8")
        {
            this.RadioButtonList1.Items[0].Selected = false;
            this.RadioButtonList1.Items[1].Selected = false;
            this.RadioButtonList1.Items[2].Selected = false;
            this.RadioButtonList1.Items[3].Selected = false;
            this.RadioButtonList1.Items[4].Selected = false;
            this.RadioButtonList1.Items[5].Selected = false;
            this.RadioButtonList1.Items[6].Selected = false;
            this.RadioButtonList1.Items[7].Selected = false;
            this.RadioButtonList1.Items[8].Selected = true;
        }
        #endregion

        if (dt.Rows[0]["ad_Lock"].ToString() == "1")
        {
            CheckBox1.Checked = true;
        }
        else
        {
            CheckBox1.Checked = false;
        }
    }
    protected void Upad_Click(object sender, EventArgs e)//上传图片
    {
        if (FileUpload1.PostedFile.ContentLength < 500000)//100000为100K
        {

            string fileFullname = this.FileUpload1.FileName;
            string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
            string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") + 1);
            string type = fileFullname.Substring(fileFullname.LastIndexOf(".") + 1);
            if (type == "bmp" || type == "jpg" || type == "gif" || type == "JPG" || type == "BMP" || type == "GIF")
            {
                this.FileUpload1.SaveAs(Server.MapPath("../Images") + "\\" + dataName + "." + type);
                string ProImg = "Images/" + dataName + "." + type;
                this.TextBox2.Text = "Images/" + dataName + "." + type;
            }
            else
            {
                Response.Write("<script language='javascript'>alert('支持格式：|jpg|gif|bmp|');</script>");
            }
        }
        else
        {
            Response.Write("<script language='javascript'>alert('你的图片已经超过500K的大小！');</script>");
        }
    }
    public void ad_add()//添加
    {
        string ad_Lock;
        string ad_Url = TextBox1.Text;
        string ad_Img = TextBox2.Text;
        int ad_Location = Convert.ToInt32(RadioButtonList1.SelectedValue);
        DateTime ad_Time = DateTime.Now;
        if (this.CheckBox1.Checked)
        {
            ad_Lock = "1";

        }
        else
        {

            ad_Lock = "0";
        }
        if (ad_Img == "")
        {
            Class1.AlertShow("请上传图片！");
            return;
        }
        adSql = "insert into AD(ad_Lock,ad_Url,ad_Img,ad_Location,ad_Time)values('" + ad_Lock + "','" + ad_Url + "','" + ad_Img + "','" + ad_Location + "','" + ad_Time + "')";
        Class1.ExecSql(adSql);
        Class1.AlertShow("发布成功！", "AD_Operation.aspx?ad=add");
    }
    public void ad_edit()//修改
    {
        int id = int.Parse(Request.QueryString["ID"]);
        string ad_Lock;
        string ad_Url = TextBox1.Text;
        string ad_Img = TextBox2.Text;
        int ad_Location = Convert.ToInt32(RadioButtonList1.SelectedValue);
        DateTime ad_Time = DateTime.Now;
        if (this.CheckBox1.Checked)
        {
            ad_Lock = "1";

        }
        else
        {

            ad_Lock = "0";
        }
        if (ad_Img == "")
        {
            Class1.AlertShow("请上传图片！");
            return;
        }

        string sql = ("update AD set ad_Lock='" + ad_Lock + "',ad_Url='" + ad_Url + "',ad_Img='" + ad_Img + "',ad_Location='" + ad_Location + "',ad_Time='" + ad_Time + "' where ad_ID=" + id + "");
        Class1.ExecSql(sql);
        Class1.AlertShow("修改成功！", "AD_Manage.aspx");

    }
    protected void btnAdd_AD_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["ad"] == "add")
        {
            ad_add();
        }
        else if (Request.QueryString["ad"] == "edit")
        {
            ad_edit();
        }
    }
}
